package com.liang.maogouyuyin.ui.viewmodel

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.liang.maogouyuyin.bean.AnomalyBean
import com.liang.maogouyuyin.local.LocalRepository
import kotlinx.coroutines.launch

class AnomalyViewModel: ViewModel() {
    private val _anomalyList = MutableLiveData<ArrayList<AnomalyBean>>().apply { value = ArrayList() }
    val anomalyList get() = _anomalyList

    fun saveAnomaly(name: String, anomalyName: String, date: Long, content: String, image: String) {
        val bean = AnomalyBean(
            name, anomalyName, date, content, image
        )
        viewModelScope.launch {
            LocalRepository.saveAnomaly(bean)
        }
    }

    fun deleteAnomaly(bean: AnomalyBean) {
        viewModelScope.launch {
            LocalRepository.deleteAnomaly(bean)
        }
    }

    fun updateAnomaly(bean: AnomalyBean) {
        viewModelScope.launch {
            LocalRepository.updateAnomaly(bean)
        }
    }

    fun loadAnomalyByName(name: String) {
        viewModelScope.launch {
            _anomalyList.value = LocalRepository.loadAnomaly(name)
        }
    }
}